{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 案例1: 检测异常服务器\n",
    "## 数据集：data/ex8data1.mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.io as sio\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['__header__', '__version__', '__globals__', 'X', 'Xval', 'yval'])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat = sio.loadmat('data/ex8data1.mat')\n",
    "mat.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((307, 2), (307, 2), (307, 1))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = mat['X']\n",
    "Xval, yval = mat['Xval'], mat['yval']\n",
    "X.shape, Xval.shape, yval.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAD8CAYAAABw1c+bAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3X+QHWWd7/H3NzNJhglJCGYISUgMezeyilxJMgkRLlxHDJKQG9TSiAVZSr13ZEpL2XWLhWsFrFDWboLDLXfdCheRFV1EY0mURRBmZbbU3cWdCYZfG5UfF5aYSOKCgKKrge/94+nH7nPSZ6Zn5syZM9OfV1VXn/51+pmTk28/59tPP4+5OyIiUh7TJroA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l0Q7LvIuCuob4XDSzjl5Lv2kOE4LWwuozJ8gZC65AnxquMeeVL1n8hfv8y+07UZ/jfCOmZhzL/rhua6fs4RBmb6vs43KQnd0VESmZKpHpERKQ4BX4RkZJR4BcRKRkFfhGRklHgFxEpGQV+EZGSUeAXESkZBX4RkZL5/8jpeXbbtJlTAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(X[:,0], X[:,1], 'bx')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 获取训练集中样本特征的均值和方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def estimateGaussian(X,isCovariance):\n",
    "    means = np.mean(X,axis=0)\n",
    "    if isCovariance:\n",
    "        sigma2 = (X-means).T@(X-means) / len(X)\n",
    "    else:\n",
    "        sigma2 = np.var(X,axis=0)\n",
    "    return means,sigma2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.83263141, -0.22712233],\n",
       "       [-0.22712233,  1.70974533]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "means,sigma2 = estimateGaussian(X,isCovariance = True)\n",
    "sigma2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.83263141, 1.70974533])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "means,sigma2 = estimateGaussian(X,isCovariance = False)\n",
    "sigma2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 多元正态分布密度函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gaussian(X,means,sigma2):\n",
    "    \n",
    "    if np.ndim(sigma2) == 1:\n",
    "        sigma2 = np.diag(sigma2)\n",
    "     \n",
    "    X = X - means\n",
    "    n = X.shape[0]\n",
    "    \n",
    "    first = np.power(2*np.pi,-n/2)*(np.linalg.det(sigma2)**(-0.5))\n",
    "    second =np.diag(X@np.linalg.inv(sigma2)@X.T) \n",
    "    p = first * np.exp(-0.5*second)\n",
    "    p = p.reshape(-1,1)\n",
    "    \n",
    "#     m = len(X)\n",
    "#     second = np.zeros(m,1)\n",
    "#     for row in range(m):\n",
    "#         second[row] = X[row].T @ np.linalg.inv(sigma2) @ X[row]\n",
    "#     p = first * np.exp(-0.5*second)\n",
    "\n",
    "    return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
